package com.itjin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itjin.model.domain.entity.CampusActivity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 校园活动Mapper
 */
@Mapper
public interface CampusActivityMapper extends BaseMapper<CampusActivity> {

    /**
     * 根据活动类型查询活动列表
     * @param activityType 活动类型
     * @param limit 限制数量
     * @return 活动列表
     */
    @Select("SELECT * FROM campus_activity WHERE activityType = #{activityType} AND isDelete = 0 " +
            "ORDER BY activityTime DESC LIMIT #{limit}")
    List<CampusActivity> findByActivityType(@Param("activityType") Integer activityType, 
                                          @Param("limit") Integer limit);

    /**
     * 根据举办人ID查询活动列表
     * @param organizerId 举办人ID
     * @return 活动列表
     */
    @Select("SELECT * FROM campus_activity WHERE organizerId = #{organizerId} AND isDelete = 0 " +
            "ORDER BY createTime DESC")
    List<CampusActivity> findByOrganizerId(@Param("organizerId") Integer organizerId);

    /**
     * 查询用户参与的活动
     * @param userId 用户ID
     * @return 活动列表
     */
    @Select("SELECT * FROM campus_activity WHERE isDelete = 0 " +
            "AND JSON_CONTAINS(participantIds, CAST(#{userId} AS JSON), '$') " +
            "ORDER BY activityTime DESC")
    List<CampusActivity> findByParticipantId(@Param("userId") Integer userId);

    /**
     * 更新活动参与人数
     * @param activityId 活动ID
     * @param currentParticipants 当前参与人数
     * @return 更新结果
     */
    @Update("UPDATE campus_activity SET currentParticipants = #{currentParticipants}, " +
            "updateTime = NOW() WHERE id = #{activityId}")
    int updateParticipantCount(@Param("activityId") Integer activityId, 
                              @Param("currentParticipants") Integer currentParticipants);

    /**
     * 查询即将开始的活动（未来24小时内）
     * @return 活动列表
     */
    @Select("SELECT * FROM campus_activity WHERE isDelete = 0 AND activityStatus = 0 " +
            "AND activityTime > NOW() AND activityTime <= DATE_ADD(NOW(), INTERVAL 24 HOUR) " +
            "ORDER BY activityTime ASC")
    List<CampusActivity> findUpcomingActivities();

    /**
     * 查询热门活动（按参与人数排序）
     * @param limit 限制数量
     * @return 活动列表
     */
    @Select("SELECT * FROM campus_activity WHERE isDelete = 0 AND activityStatus = 0 " +
            "AND activityTime > NOW() " +
            "ORDER BY currentParticipants DESC, createTime DESC LIMIT #{limit}")
    List<CampusActivity> findPopularActivities(@Param("limit") Integer limit);
} 